package pkg.chemdrm;

import javax.microedition.khronos.opengles.GL10;

import org.anddev.andengine.entity.sprite.AnimatedSprite;
import org.anddev.andengine.entity.sprite.Sprite;
import org.anddev.andengine.opengl.texture.region.TextureRegion;
import org.anddev.andengine.opengl.texture.region.TiledTextureRegion;

public class Player extends AnimatedSprite implements Entity {

	public final static int PLAYER_DIR_UP = 0;
	public final static int PLAYER_DIR_UP_RIGHT = 2;
	public final static int PLAYER_DIR_RIGHT = 4;
	public final static int PLAYER_DIR_DOWN_RIGHT = 6;
	public final static int PLAYER_DIR_DOWN = 8;
	public final static int PLAYER_DIR_DOWN_LEFT = 10;
	public final static int PLAYER_DIR_LEFT = 12;
	public final static int PLAYER_DIR_UP_LEFT = 14;
	
	
	
	protected int mPlayerMaxHealth = 100;
	protected float mPlayerVelocity = 256f; // 3 tiles per second
	
	protected int mHealth;
	
	protected float mPlayerXVelocity = 0;
	protected float mPlayerYVelocity = 0;
	
	private EntityTorch mPlayerTorch = null;
	
	// let the hacks begin!
	private Map mMapInfo; 
	
	boolean mIsCaffeinated = false;
	boolean mHasTorch = false;
	
	private int mDirection = 0;
	boolean moveFlag = false;
		
	protected ResourceHandler mResourceHandler = null;
	
	public Player(ResourceHandler pResourceHandler, Map pMapInfo)
	{
		super(100, 100, 51, 98, pResourceHandler.getAnimatedRegion("player_walk"));

		mResourceHandler = pResourceHandler;
		mMapInfo = pMapInfo;
		
		setTorch(false);
		
	//	this.attachChild(mPlayerTorch); // I think the coordinates will be relative to this.
	}
	
	public Player(float pX, float pY, float pWidth, float pHeight, TiledTextureRegion pTextureRegion) 
	{
		super(pX, pY, pWidth, pHeight, pTextureRegion);
		
	}
	
	public void setTorch(boolean t)
	{
		mHasTorch = t;
		
		if(t)
		{
			if(mPlayerTorch == null)
			{
				mPlayerTorch = new EntityTorch(-256, -256, 512, 512, mResourceHandler.getRegion("glow"));		
				attachChild(mPlayerTorch);
			}
		}
		else
		{
			if(mPlayerTorch != null)
			{
				this.detachChild(mPlayerTorch);	
				mPlayerTorch.detachSelf();
				mPlayerTorch = null;
			}
		}
	}
	protected void checkHealthBounds()
	{
		if(mHealth > mPlayerMaxHealth)
		{
			mHealth = mPlayerMaxHealth;
		}
		else if(mHealth < 0)
		{
			mHealth = 0;
		}
	}
	
	public boolean getIsMoving()
	{
		if(mPlayerXVelocity != 0 || mPlayerYVelocity != 0)
		{
			return true;
		}
		return false;
	}
	
	public void setHealth(int pValue)
	{
		mHealth = pValue;
		checkHealthBounds();
		
	}
	
	public void changeHealthBy(int pValue)
	{
		mHealth += pValue;
		checkHealthBounds();
	}
	
	public int getHealth()
	{
		return mHealth;
	}
	
	public void setCaffeinated(boolean b)
	{
		mIsCaffeinated = b;
	}
	
	public void update(float dt)
	{		
		float tX = getX(), tY = getY();
		// update our position if we're moving.
		mX += mPlayerXVelocity*dt;
		mY += mPlayerYVelocity*dt;
		
		
		// check
		if(mMapInfo.checkCollision(this))
		{
			mX = tX;
			mY = tY;
			return;
		}
		
		// torch flickerin
		if(mPlayerTorch != null)
			mPlayerTorch.update(dt);
		
	}
	
	public void setMove(int pX, int pY, int dir)
	{
		/* This locks the size of pX and pY to 1. */
		if(Math.abs(pX) > 1)
			pX /= Math.abs(pX);
		if(Math.abs(pY) > 1)
			pY /= Math.abs(pY);
		
		if(mIsCaffeinated)
			mPlayerVelocity = 96*2+64;
		else
			mPlayerVelocity = 256f;
		
		if(Math.abs(pX) > 0 && Math.abs(pY) > 0)
		{
			mPlayerXVelocity = (float) (mPlayerVelocity * 0.7071)*pX;
			mPlayerYVelocity = (float) (mPlayerVelocity * 0.7071)*pY;
		}
		else
		{
			mPlayerXVelocity = (float) (mPlayerVelocity)*pX;
			mPlayerYVelocity = (float) (mPlayerVelocity)*pY;
		}
		
		// hopefully whoever is passing this in uses it correctly. Use the static ints defined at the top of Player.
	
		if(this.getIsMoving())
		{
			moveFlag = true;
			if(dir != mDirection)
				this.animate(new long[]{200, 200}, dir, dir+1, true);
			
		}
		
		mDirection = dir;

		
	}
	
	public void stopMoving()
	{
		mPlayerXVelocity = 0;
		mPlayerYVelocity = 0;
		
		this.stopAnimation(mDirection);
		moveFlag = false;
	}

	public boolean getExists() 
	{
		return true;
	}
	
}
